package com.oa.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.BulletinAddDTO;
import com.oa.admin.dto.BulletinListDTO;
import com.oa.admin.entity.BulletinFile;
import com.oa.admin.service.IBulletinFileService;
import com.oa.admin.vo.BulletinListDetailsVO;
import com.oa.admin.vo.BulletinListVO;
import com.oa.core.PageResult;
import com.oa.admin.entity.Bulletin;
import com.oa.admin.mapper.BulletinMapper;
import com.oa.admin.service.IBulletinService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.core.utils.SendMsgManager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 公告管理 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class BulletinServiceImpl extends ServiceImpl<BulletinMapper, Bulletin> implements IBulletinService {

    private final BulletinMapper bulletinMapper;

    @Autowired
    public BulletinServiceImpl(BulletinMapper bulletinMapper) {
        this.bulletinMapper = bulletinMapper;
    }

    @Autowired
    private IBulletinFileService bulletinFileService;

    @Autowired
    private SendMsgManager sendMsgManager;

    @Override
    @CommonService
    public PageResult<Bulletin> findList(Integer pageNum, Integer pageSize) {
        if(pageNum == null){
            List<Bulletin> list = bulletinMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<Bulletin> page = new Page<>(pageNum, pageSize);
            //查询
            List<Bulletin> list = bulletinMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public Bulletin findBulletinById(String id){
        return bulletinMapper.findById(id);
    }

    @CommonService
    @Override
    public PageResult<BulletinListVO> findBulletinList(BulletinListDTO bulletinListDTO) {
        Page page = bulletinListDTO.getPage();
        return new PageResult<>(bulletinMapper.findBulletinList(page, bulletinListDTO), page);
    }

    @CommonService
    @Override
    public BulletinListDetailsVO findBulletinListDetails(String id) {
        return bulletinMapper.findBulletinListDetails(id);
    }

    @Transactional
    @CommonService
    @Override
    public Resp add(BulletinAddDTO bulletinAddDTO) {
        Bulletin bulletin = new Bulletin();
        BeanUtils.copyProperties(bulletinAddDTO, bulletin);
        if(insert(bulletin)){
            if(bulletinAddDTO.getBulletinFiles() != null && bulletinAddDTO.getBulletinFiles().size() > 0){
                List<BulletinFile> bulletinFiles = new ArrayList<>();
                bulletinAddDTO.getBulletinFiles().forEach(d -> {
                    BulletinFile bulletinFile = new BulletinFile();
                    BeanUtils.copyProperties(d, bulletinFile);
                    bulletinFile.setBulletinId(bulletin.getId());
                    bulletinFile.setUploadTime(new Date());
                    bulletinFiles.add(bulletinFile);
                });
                if(bulletinFileService.insertBatch(bulletinFiles)){
                    return new Resp().success("添加成功");
                }
                return new Resp().failure();
            }
            sendMsgManager.sendMsgAll("新公告发布", bulletinAddDTO.getTitle());
            return new Resp().success("添加成功");
        }
        return new Resp().failure();
    }

    @Transactional
    @CommonService
    @Override
    public Resp edit(BulletinAddDTO bulletinAddDTO) {
        Bulletin bulletin = new Bulletin();
        BeanUtils.copyProperties(bulletinAddDTO, bulletin);
        if(updateById(bulletin)){
            if(bulletinAddDTO.getBulletinFiles() != null && bulletinAddDTO.getBulletinFiles().size() > 0){
                bulletinFileService.delete(new EntityWrapper().eq("bulletin_id", bulletin.getId()));
                List<BulletinFile> bulletinFiles = new ArrayList<>();
                bulletinAddDTO.getBulletinFiles().forEach(d -> {
                    BulletinFile bulletinFile = new BulletinFile();
                    BeanUtils.copyProperties(d, bulletinFile);
                    bulletinFile.setBulletinId(bulletin.getId());
                    bulletinFile.setUploadTime(new Date());
                    bulletinFiles.add(bulletinFile);
                });
                if(bulletinFileService.insertBatch(bulletinFiles)){
                    return new Resp().success("修改成功");
                }
                return new Resp().failure();
            }
            return new Resp().success("修改成功");
        }
        return new Resp().failure();
    }
}
